package com.beiben.trialproduce.samplecar.controller;

import java.util.List;

import com.beiben.common.GlobalConstant;
import com.beiben.experiment.equipment.domain.StateVo;
import com.beiben.experiment.repair.domain.Repair;
import com.beiben.system.file.domain.File;
import com.beiben.system.file.service.IFileService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.beiben.trialproduce.samplecar.domain.SampleCar;
import com.beiben.trialproduce.samplecar.service.ISampleCarService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 样车Controller
 * 
 * @author 熊国强
 * @date 2020-11-18
 */
@RestController
@RequestMapping("/trialproduce/car")
public class SampleCarController extends BaseController
{
    @Autowired
    private ISampleCarService sampleCarService;

    @Autowired
    private IFileService fileService;

    /**
     * 查询样车列表
     */
    @PreAuthorize("@ss.hasPermi('trialproduce:car:list')")
    @GetMapping("/list")
    public TableDataInfo list(SampleCar sampleCar)
    {
        startPage();
        List<SampleCar> list = sampleCarService.selectSampleCarList(sampleCar);
        return getDataTable(list);
    }

    /**
     * 导出样车列表
     */
    @PreAuthorize("@ss.hasPermi('trialproduce:car:export')")
    @Log(title = "样车", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SampleCar sampleCar)
    {
        return sampleCarService.downLoadSampleCarExcel(sampleCar);
    }


    /**
     * 获取样车详细信息
     */
    @PreAuthorize("@ss.hasPermi('trialproduce:car:query')")
    @GetMapping(value = "/{carId}")
    public AjaxResult getInfo(@PathVariable("carId") Long carId)
    {
        return AjaxResult.success(sampleCarService.selectSampleCarById(carId));
    }

    /**
     * 新增样车
     */
    @PreAuthorize("@ss.hasPermi('trialproduce:car:add')")
    @Log(title = "样车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SampleCar sampleCar)
    {
        return toAjax(sampleCarService.insertSampleCar(sampleCar));
    }

    /**
     * 修改样车
     */
    @PreAuthorize("@ss.hasPermi('trialproduce:car:edit')")
    @Log(title = "样车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SampleCar sampleCar)
    {
        return toAjax(sampleCarService.updateSampleCar(sampleCar));
    }

    /**
     * 删除样车
     */
    @PreAuthorize("@ss.hasPermi('trialproduce:car:remove')")
    @Log(title = "样车", businessType = BusinessType.DELETE)
	@DeleteMapping("/{carIds}")
    public AjaxResult remove(@PathVariable Long[] carIds)
    {
        return toAjax(sampleCarService.deleteSampleCarByIds(carIds));
    }

    /**
     * 导入样车信息
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<SampleCar> util = new ExcelUtil<SampleCar>(SampleCar.class);
        return util.importTemplateExcel("样车数据");
    }

    @Log(title = "样车导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('trialproduce:car:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SampleCar> util = new ExcelUtil<SampleCar>(SampleCar.class);
        List<SampleCar> sampleCartList = util.importExcel(file.getInputStream());
        String message = sampleCarService.importSampleCar(sampleCartList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 上传图片
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile[] file, Long tableId,String imagName) {
        try {
            fileService.uploadFileName(file, tableId, GlobalConstant.TRIAL_PRODUCE_SAMPLE_CAR,GlobalConstant.TRIAL_PRODUCE_SAMPLE_CAR_ADDRESS,imagName);
            AjaxResult ajax = AjaxResult.success();
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 按Id获取文件列表
     *
     * @return
     */
    @PostMapping("/files/{carId}")
    public TableDataInfo selectFiles(@PathVariable Long carId) {
        startPage();
        List<File> list = sampleCarService.selectFiles(carId);
        return getDataTable(list);
    }

    /**
     * 修改样车 使用状态
     */
    @PostMapping("/changeState")
    public AjaxResult changeState(@RequestBody Repair repair)
    {
        return toAjax(sampleCarService.updateSampleCarState(repair));
    }
}
